package com.pda.xinmazhi.ui.backstorage;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.xinmazhi.R;
import com.pda.xinmazhi.bean.LatheMachinBean;
import com.pda.xinmazhi.bean.ScanBean;
import com.pda.xinmazhi.bean.ScanResultBean;
import com.pda.xinmazhi.bean.SelectBean;
import com.pda.xinmazhi.bean.SurplusDetailBean;
import com.pda.xinmazhi.bean.request.BackConfirmRequest;
import com.pda.xinmazhi.bean.request.QRcodeRequest;
import com.pda.xinmazhi.bean.request.SurplusConfirmRequest;
import com.pda.xinmazhi.contract.BackStorageContract;
import com.pda.xinmazhi.dialog.AffirmDialog;
import com.pda.xinmazhi.net.NetUtils;
import com.pda.xinmazhi.presenter.BackStoragePresenter;
import com.pda.xinmazhi.utils.PopUtisl;
import com.pda.xinmazhi.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
@Route(path = AppActivityKey.BACKSTORAGEACTIVITY)
public class BackStorageActivity extends BaseMvpActivity<BackStoragePresenter> implements BackStorageContract.View {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_latheCode)
    TextView tvLatheCode;
    @BindView(R.id.tv_connectionCode)
    TextView tvConnectionCode;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.tv_container)
    TextView tvContainer;
    @BindView(R.id.tv_articleId)
    TextView tvArticleId;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    @BindView(R.id.tv_batch)
    TextView tvBatch;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.et_surplusWeight)
    EditText etSurplusWeight;
    @BindView(R.id.tv_unit)
    TextView tvUnit;

    @Override
    public BackStoragePresenter getPresenter() {
        return new BackStoragePresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_back_storage;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                getCode(request);
            }
        });
    }

    public void getCode(QRcodeRequest request) {
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> stringBaseAppEntity) throws Exception {
                        if (!stringBaseAppEntity.getCode().equals("200")) {
                            ToastUtils.showToast(mContext, stringBaseAppEntity.getMessage());
                            return;
                        }
                        if (stringBaseAppEntity.getData().getType().equals("KW")) {
                            tvConnectionCode.setText(stringBaseAppEntity.getData().getCode());
                            // tvStorageBin.setText(stringBaseAppEntity.getData().getCode());
                            //SharedPreferencesUtil.getInstance().putString(StringKey.STORAGEBIN,stringBaseAppEntity.getData().getCode());
                        } /*else if (stringBaseAppEntity.getData().getType().equals("ZT")){
                            tvContainer.setText(stringBaseAppEntity.getData().getCode());
                        }*/
                        else {
                            ToastUtils.showToast(mContext, "请扫正确的条形码");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.showToast(mContext, throwable.getMessage());
                    }
                });
    }

    @Override
    public void getData() {

    }


    @OnClick({R.id.iv_close, R.id.tv_balance, R.id.tv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finishActivity();
                break;
            case R.id.tv_balance:
                if (TextUtils.isEmpty(tvLatheCode.getText().toString())) {
                    showMessage("请先选择机台");
                    return;
                }
                if (TextUtils.isEmpty(tvContainer.getText().toString())) {
                    showMessage("请先扫锥桶");
                    return;
                }

                AffirmDialog dialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        SurplusConfirmRequest request = new SurplusConfirmRequest();
                        request.setContainer(tvContainer.getText().toString());
                        request.setSurplusWeight(etSurplusWeight.getText().toString());
                        presenter.surplusConfirm(request);
                    }
                });
                dialog.show("确定要结余吗？");
                break;
            case R.id.tv_back:
                if (TextUtils.isEmpty(tvContainer.getText().toString())) {
                    showMessage("请先扫锥桶");
                    return;
                }
                if (TextUtils.isEmpty(tvConnectionCode.getText().toString())) {
                    showMessage("请先扫接驳位");
                    return;
                }
                AffirmDialog affirmDialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        BackConfirmRequest request1=new BackConfirmRequest();
                        request1.setConnectionCode(tvConnectionCode.getText().toString());
                        request1.setContainer(tvContainer.getText().toString());
                        presenter.backConfirm(request1);
                    }
                });
                affirmDialog.show("确定要回库吗？");

                break;
        }
    }

    @Override
    public void setData(SurplusDetailBean surplusDetailBean) {
        tvArticleId.setText(surplusDetailBean.getArticleId());
        tvContainer.setText(surplusDetailBean.getContainer());
        tvGrade.setText(surplusDetailBean.getGrade());
        tvBatch.setText(surplusDetailBean.getBatch());
        etSurplusWeight.setText(surplusDetailBean.getSurplusWeight() + "");
        tvUnit.setText(surplusDetailBean.getUnit());
        if (surplusDetailBean.getButtonShow().equals("10")){
            tvBalance.setVisibility(View.VISIBLE);
            tvBack.setVisibility(View.GONE);
        }
        if (surplusDetailBean.getButtonShow().equals("20")){
            tvBalance.setVisibility(View.GONE);
            tvBack.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onSuccess() {
        latheBean=null;
        tvLatheCode.setText("");
        tvConnectionCode.setText("");
        tvArticleId.setText("");
        tvContainer.setText("");
        tvGrade.setText("");
        tvBatch.setText("");
        etSurplusWeight.setText("");
        tvUnit.setText("");
    }

    SelectBean latheBean;

    @Override
    public void setList(List<LatheMachinBean> list) {
        List<SelectBean> selectBeans = new ArrayList<>();
        for (LatheMachinBean bean : list) {
            selectBeans.add(new SelectBean(bean.getLatheName(), bean.getLatheCode()));
        }
        PopUtisl.showSelectPop(this, selectBeans, tvLatheCode, new PopUtisl.SelectClick() {
            @Override
            public void onClick(SelectBean bean) {
                latheBean = bean;
                tvLatheCode.setText(bean.getName());
                presenter.getSurplusDetail(latheBean.getId());
            }
        }, "请选择机台");
    }

    @OnClick(R.id.tv_latheCode)
    public void onViewClicked() {
        presenter.getLatheListByCenter("JM1001");
    }

}
